#include "../include/common.h"

int recvmasge(int netfd, TLVMsg *msg)
{
    bzero(msg, sizeof(TLVMsg));
    int rest = 0;
    rest = recv(netfd, &msg->type, sizeof(int), MSG_WAITALL);
    if (rest == 0)
    {
        return -1;
    }
    rest = recv(netfd, &msg->value_len, sizeof(int), MSG_WAITALL);
    if (rest == 0)
    {
        return -1;
    }

    rest = recv(netfd, msg->value, msg->value_len, MSG_WAITALL);
    // printf("fdsfadf收到消息：type=%d, value_len=%d, value=%s\n", msg->type, msg->value_len, msg->value);
    if (rest == 0)
    {
        return -1;
    }

    msg->value[msg->value_len + 1] = '\0'; // 确保字符串结束
    // printf("收到消息：type=%d, value_len=%d, value=%s\n", msg->type, msg->value_len, msg->value);
    return 0;
}

int sendmasge(int netfd, int cont, int len, char *buf)
{
    TLVMsg sendmsg;
    bzero(&sendmsg, sizeof(TLVMsg));
    sendmsg.type = cont;
    sendmsg.value_len = len;
    memcpy(sendmsg.value, buf, len);
    sendmsg.value[sendmsg.value_len + 1] = '\0';

    send(netfd, &sendmsg.type, sizeof(int), MSG_NOSIGNAL);
    send(netfd, &sendmsg.value_len, sizeof(int), MSG_NOSIGNAL);
    send(netfd, &sendmsg.value, sendmsg.value_len, MSG_NOSIGNAL);
    printf("发送消息：type:%d,len:%d,value:%s\n", sendmsg.type, sendmsg.value_len, sendmsg.value);
    return 0;
}

void *worker_function(void *arg)
{
    threadpool_t *threadpool = (threadpool_t *)arg;

    TLVMsg msg;
    int netfd = 0;
    char error_buf[1024];
    char buf[1024];
    user_t user;
    int sret = -1;
    MYSQL *mysql = mysql_init(NULL);
    con_mysql(mysql);
    while (1)
    {
        pthread_mutex_lock(&threadpool->mutex);
       
        while (threadpool->queue.cond == 0)
        {
            pthread_cond_wait(&threadpool->cond, &threadpool->mutex);
        }
        netfd = threadpool->queue.head->fd;
        del_queue(&threadpool->queue);
        pthread_mutex_unlock(&threadpool->mutex);
        int ret_recv = -1;

        // char steser[]="服务端与客户端正常连接\0";
        // sendmasge(netfd,14,strlen(steser),steser);
        // 1.进行登录
        session_t session;
        while (1)
        {
            // sendmasge(netfd,14,strlen(steser),steser);
            ret_recv = recvmasge(netfd, &msg);
            if (ret_recv != 0)
            {
                break;
            }
            // printf("------------type=%d,len=%d,value=%s\n", msg.type, msg.value_len, msg.value);
            switch (msg.type)
            {
            case CMD_LOGIN:
                 printf("this is sign \n");
                {
                    ret_recv = server_login_in(&user, netfd, mysql);
                    if (ret_recv == 0)
                    {
                        init_session(&session, user.username, mysql);
                        break;
                    }
                    break;
                }
            case CMD_SIGN:
                printf("this is sign \n");
                {
                    ret_recv = server_sign_in(&user, netfd, mysql);
                    if (ret_recv == 0)
                    {
                        char buf[20] = "sign in succee\0";
                        sendmasge(netfd, CMD_SIGN, strlen(buf), buf);
                        PRINT_LOG_FILE(threadpool->logfd, user.username, 8, "sign in  succes", "/", threadpool->mutex);
                        init_session(&session, user.username, mysql);
                        break;
                    }
                    else
                    {
                        char buf[64] = "The account already exists.\0";
                        PRINT_LOG_FILE(threadpool->logfd, user.username, 8, "sign in  failed", "/", threadpool->mutex);
                        sendmasge(netfd, CMD_SIGN, strlen(buf), buf);
                        break;
                    }
                }
            case (CMD_PUTS): // 上传文件
                recvfile_server(netfd, mysql,user.username,session.curDirID, session.curVirPath);
                break;
            case (CMD_GETS): // 上传文件
                printf("adf-----------nnsg=%s\n",msg.value);
                transfile_server(msg.value,netfd);
                break;
            case CMD_CD: // 切换目录
                printf("this is cd \n");
                handle_cd(&session,mysql,netfd,msg.value);
                break;
            case CMD_PWD:
                printf("this is pwd \n");
                handle_pwd(&session,netfd);
                break; // 显示路径
            case CMD_MKDIR:
                printf("this is mkdir \n");
                handle_mkdir(&session,mysql,netfd,msg.value);
                break;
            case CMD_LS: // 列出文件
                printf("this is ls \n");
                handle_ls(&session,mysql,netfd,msg.value);
                break;
            case CMD_RMDIR:
                printf("this is rmdir \n");
                handle_rmdir(&session,mysql,netfd,msg.value);
                break;
            case CMD_REMOVE:
                printf("this is remove \n");
                handle_remove(&session,mysql,netfd,msg.value);
                break; // 删除文件
            default:
                break;
            }

        }
        close(netfd);
        printf("关闭连接: %d\n", netfd);
    }
}
int creat_worker(threadpool_t *threadpool){
    for (int i = 0; i < threadpool->thread_arr.workNum; i++)
    {
        pthread_create(&threadpool->thread_arr.threadtid[i], NULL, worker_function, threadpool);
    }

    return 0;
}
